﻿
Partial Class WebForm_ManageProduct_ManageMaintainLevel
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack() Then
            Me.BindDDLDivision()
            Me.BindDDLViewPoint()
        End If
    End Sub

    Protected Sub btSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btSubmit.Click
        Me.SetSignatureTop()
        Me.BindGV()

        Me.btnup.Visible = True
        Me.btndown.Visible = True
        Me.btnApply.Visible = True
        Me.plList.Visible = True
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            If DirectCast(e.Row.FindControl("lblSelect"), Label).Text = "Y" Then
                DirectCast(e.Row.FindControl("chkSelect"), CheckBox).Checked = True
            End If

            If DirectCast(e.Row.FindControl("lblCheck"), Label).Text = "Y" Then
                DirectCast(e.Row.FindControl("Radio"), HtmlInputRadioButton).Checked = True
                e.Row.BackColor = Drawing.Color.LightGreen
            End If

            If e.Row.RowIndex = 0 Then
                DirectCast(e.Row.FindControl("chkSelect"), CheckBox).Enabled = False
                DirectCast(e.Row.FindControl("Radio"), HtmlInputRadioButton).Visible = False
            End If



        End If
    End Sub

    Protected Sub btnup_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnup.Click
        Me.GetSelectID()


        If RowIndex - 1 = 0 Then
            MyObj.ShowMessage("It can not move to top.")
            'ElseIf IsSig(gvID) = True Then
            '    MyObj.ShowMessage("It is the top.")
        Else
            Me.gvChangeID = Me.gv.DataKeys(RowIndex - 1)("ID").ToString()

            '將原本位置順序更新,亦為改為欲移位置的順序(gvID = 原本位置ID )
            Me.UpdateSequence(DT.Rows(RowIndex)("Sequence") - 1, gvID)
            '將欲移位置順序更新,亦為改為原本位置的順序(gvChangeID = 欲移位置ID)
            Me.UpdateSequence(DT.Rows(RowIndex - 1)("Sequence") + 1, gvChangeID)

            Me.BindGV()
        End If

    End Sub

    Protected Sub btndown_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndown.Click
        Me.GetSelectID()

        If RowIndex + 1 = DT.Rows.Count Then
            MyObj.ShowMessage("It is the last.")
            'ElseIf IsSig(gvID) = True Then
            '    MyObj.ShowMessage("It can not move.Signature should be top.")
        Else
            Me.gvChangeID = Me.gv.DataKeys(RowIndex + 1)("ID").ToString()
            '將欲移位置順序更新,亦為改為原本位置的順序(gvChangeID = 欲移位置ID)
            Me.UpdateSequence(DT.Rows(RowIndex + 1)("Sequence") - 1, gvChangeID)
            '將原本位置順序更新,亦為改為欲移位置的順序(gvID = 原本位置ID )
            Me.UpdateSequence(DT.Rows(RowIndex)("Sequence") + 1, gvID)


            Me.BindGV()
        End If

    End Sub

    Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Dim CheckString As String = ""
        Dim UnCheckString As String = ""
        For i As Integer = 0 To Me.gv.Rows.Count - 1
            Dim cb As CheckBox = DirectCast(Me.gv.Rows(i).FindControl("chkSelect"), CheckBox)

            If cb.Checked = True Then
                CheckString += DT.Rows(i)("ID").ToString() & ","
            Else
                UnCheckString += DT.Rows(i)("ID").ToString() & ","
            End If

            'If cb.Checked = True Then
            '    Me.UpdateIsSelect("Y", DT.Rows(i)("ID").ToString())
            'Else
            '    Me.UpdateIsSelect("N", DT.Rows(i)("ID").ToString())
            'End If
        Next

        CheckString = CheckString.Substring(0, CheckString.Length - 1)
        UnCheckString = UnCheckString.Substring(0, UnCheckString.Length - 1)

        If ddlViewPoint.SelectedValue = "TC" And CheckString.Split(",").GetLength(0) > 1 Then
            MyObj.ShowMessage("Apply failed.It can not over two levels.")
        ElseIf CheckString.Split(",").GetLength(0) < 0 Then
            MyObj.ShowMessage("Apply failed.You should choose Level.")
        Else
            For Each CS As String In CheckString.Split(",")
                Me.UpdateIsSelect("Y", CS)
            Next
            For Each UCS As String In UnCheckString.Split(",")
                Me.UpdateIsSelect("N", UCS)
            Next
        End If

    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    End Sub
#Region "方法"
    Protected Sub GetSelectID()

        For Each row As GridViewRow In gv.Rows
            Dim rCheck As HtmlInputRadioButton = DirectCast(row.FindControl("Radio"), HtmlInputRadioButton)
            If rCheck.Checked Then
                gvID = CInt(gv.DataKeys(row.RowIndex)("ID"))
                RowIndex = row.RowIndex
                Me.UpdateIsCheck(gvID)
                Exit For
            End If
        Next

    End Sub
    Protected Sub UpdateIsCheck(ByVal ID As String)
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As New Data.DataTable

        sql.Length = 0
        sql.AppendFormat("Select * From MaintainLevel ")
        sql.AppendFormat("where IsCheck = 'Y' ")
        sql.AppendFormat("and Division = '{0}' ", ddlDivision.SelectedValue)
        sql.AppendFormat("and ViewPoint = '{0}' ", ddlViewPoint.SelectedValue)
        dt = dat.GetData(sql)

        If dt.Rows.Count > 0 Then
            sql.Length = 0
            sql.AppendFormat("Update MaintainLevel Set IsCheck = 'N' ")
            sql.AppendFormat("where ID = '{0}' ", dt.Rows(0)("ID"))
            dat.AccData(sql)
        End If

        sql.Length = 0
        sql.AppendFormat("Update MaintainLevel Set IsCheck = 'Y' ")
        sql.AppendFormat("where ID = '{0}' ", ID)
        dat.AccData(sql)
    End Sub
    Protected Sub UpdateSequence(ByVal Sequence As Integer, ByVal ID As String)
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("Update MaintainLevel Set Sequence = '{0}' ", Sequence)
        sql.AppendFormat("where ID = '{0}' ", ID)
        dat.AccData(sql)

    End Sub
    Protected Sub UpdateIsSelect(ByVal IsSelect As String, ByVal ID As String)
        Dim sql As New MyStringBuilder
        Dim dat As New clsData


        sql.Length = 0
        sql.AppendFormat("Update MaintainLevel Set IsSelect = '{0}' ", IsSelect)
        sql.AppendFormat("Where ID = '{0}' ", ID)

        dat.AccData(sql)
    End Sub
    Protected Sub SetSignatureTop()
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("UPDATE MaintainLevel SET ")
        sql.AppendFormat("Sequence='1' ")
        sql.AppendFormat(",IsSelect='Y' ")
        sql.AppendFormat(",IsCheck = 'N' ")
        sql.AppendFormat("WHERE Division = '{0}' ", ddlDivision.SelectedValue)
        sql.AppendFormat("AND ViewPoint = '{0}' ", ddlViewPoint.SelectedValue)
        sql.AppendFormat("AND HierarchyName = 'Signature' ;")
        sql.AppendFormat("Update MaintainLevel SET ")
        sql.AppendFormat("IsCheck = 'Y' ")
        sql.AppendFormat("WHERE Division = '{0}' ", ddlDivision.SelectedValue)
        sql.AppendFormat("AND ViewPoint = '{0}' ", ddlViewPoint.SelectedValue)
        sql.AppendFormat("AND Sequence='2' ")
        sql.AppendFormat("AND not exists( ")
        sql.AppendFormat("SELECT IsCheck FROM MaintainLevel ")
        sql.AppendFormat("WHERE IsCheck='Y' ")
        sql.AppendFormat("AND Division = '{0}' ", ddlDivision.SelectedValue)
        sql.AppendFormat("AND ViewPoint = '{0}'); ", ddlViewPoint.SelectedValue)
        dat.AccData(sql)
    End Sub
    Protected Function IsSig(ByVal ID As Integer) As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("SELECT ID,HierarchyName,Division,ViewPoint  ")
        sql.AppendFormat("FROM MaintainLevel ")
        sql.AppendFormat("WHERE 1=1 ")
        sql.AppendFormat("AND Division = '{0}' ", ddlDivision.SelectedValue)
        sql.AppendFormat("AND ViewPoint = '{0}' ", ddlViewPoint.SelectedValue)
        sql.AppendFormat("AND ID= {0} ", ID)

        If dat.GetData(sql).Rows(0)("HierarchyName").ToString() = "Signature" Then
            Return True
        Else
            Return False
        End If

    End Function
#End Region

#Region "繫結資料"
    Protected Sub BindDDLDivision()

        ddlDivision.DataTextField = "Text"
        ddlDivision.DataValueField = "Value"

        ddlDivision.DataSource = MyObj.GetCodeMappingData("Product", "Division")
        ddlDivision.DataBind()
        ddlDivision.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
    Protected Sub BindDDLViewPoint()
        ddlViewPoint.DataTextField = "Text"
        ddlViewPoint.DataValueField = "Value"

        ddlViewPoint.DataSource = MyObj.GetCodeMappingData("Product", "ViewPoint")
        ddlViewPoint.DataBind()
        ddlViewPoint.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
    Public Sub BindGV()
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("Select m.* From MaintainLevel m ")
        sql.AppendFormat("where m.Division = '{0}' ", ddlDivision.SelectedValue)
        sql.AppendFormat("and m.ViewPoint = '{0}' ", ddlViewPoint.SelectedValue)
        sql.AppendFormat("order by m.Sequence ")
        DT = dat.GetData(sql)
        gv.DataSource = DT
        gv.DataBind()
    End Sub

#End Region

#Region "屬性"
    Public Property DT() As Data.DataTable
        Get
            Return ViewState("DT")
        End Get
        Set(ByVal value As Data.DataTable)
            ViewState("DT") = value
        End Set
    End Property
    Public Property gvID() As String
        Get
            Return ViewState("gvID")
        End Get
        Set(ByVal value As String)
            ViewState("gvID") = value
        End Set
    End Property
    Public Property gvChangeID() As String
        Get
            Return ViewState("gvChangeID")
        End Get
        Set(ByVal value As String)
            ViewState("gvChangeID") = value
        End Set
    End Property
    Public Property RowIndex() As Integer
        Get
            Return ViewState("RowIndex")
        End Get
        Set(ByVal value As Integer)
            ViewState("RowIndex") = value
        End Set
    End Property

#End Region
End Class
